var t = getApp();

Page({
    data: {
        pid: 0,
        scrollArr: [],
        package_id: null,
        isScroll: !1,
        order_active: 0,
        total: 0,
        title: "",
        share_title: "",
        share_thumb: "",
        share_desc: "",
        description_title: "",
        description_thumb: "",
        description_desc: "",
        order_list: [],
        checkAll: !1,
        carousels: [],
        on_sale_price: 0,
        end_time: 0,
        end_time_obj: {},
        time_over: !1,
        isDiscount: !1,
        other_packages: [],
        loadingHidden: !1,
        page_template: 0
    },
    getTotal: function() {
        var t = this.data.carousels.length > 0 ? [ 260 ] : [ 60 ];
        t[0] += this.data.time_over ? 80 : 0;
        var e = wx.createSelectorQuery(), a = !1;
        e.select(".info").boundingClientRect(function(e) {
            !a && e && (t[0] += e.height, a = !0);
        }).exec(), e.selectAll(".list-item").boundingClientRect(function(e) {
            e.forEach(function(e, a) {
                t[a + 1] = t[a] + e.height + 10;
            });
        }).exec(), this.setData({
            scrollArr: t
        });
    },
    count_down: function(t) {
        var e = t.detail, a = e.days, i = e.hours, s = e.seconds, o = e.minutes, n = {
            days: a,
            hours: i >= 10 ? i : "0" + i,
            seconds: s >= 10 ? s : "0" + s,
            minutes: o >= 10 ? o : "0" + o
        };
        this.setData({
            end_time_obj: n
        });
    },
    finish: function() {
        this.setData({
            time_over: !1
        }), this.totalPrice();
    },
    onPageScroll: function(t) {
        var e = this, a = t.scrollTop;
        this.data.isScroll || this.data.scrollArr.forEach(function(t, i) {
            (a > t - 10 && a < t + 10 || a < e.data.scrollArr[i + 1] - 100 && a > t) && e.setData({
                order_active: i
            });
        });
    },
    link: function(t) {
        var e = t.currentTarget.dataset.item.carousel_link;
        wx.navigateTo({
            url: e
        });
    },
    change: function(t) {
        var e = this, a = t.detail;
        this.setData({
            isScroll: !0
        });
        var i = this.data.scrollArr[a.index];
        wx.pageScrollTo({
            scrollTop: i,
            duration: 300
        }), setTimeout(function() {
            e.setData({
                isScroll: !1
            });
        }, 330);
    },
    settlement: function() {
        for (var e = [], a = {
            goods_list: [],
            package_id: this.data.package_id
        }, i = this.data.order_list, s = 0; s < i.length; s++) for (var o = i[s], n = 0; n < o.goods_list.length; n++) {
            var r = o.goods_list[n];
            if (r.check) {
                var c = r.check_spe.reduce(function(t, e) {
                    return e >= 0 ? t + 1 : t;
                }, 0);
                if (!this.data.page_template && 1 == r.has_option && c != r.has_many_specs.length) return t.tips("请选择商品规格"), 
                !1;
                e.push(r.id), a.goods_list.push({
                    goods_id: r.id,
                    total: r.count,
                    option_id: r.spe_name || ""
                });
            }
        }
        if (e.length <= 0) return t.tips("请选择结算商品");
        wx.navigateTo({
            url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=packagBuy&cart_ids=" + JSON.stringify(e) + "&packagJson=" + JSON.stringify(a)
        });
    },
    tapSpe: function(t) {
        var e = t.target.dataset, a = e.i, i = e.c_index, s = e.spe_index, o = e.spe_item_index, n = this.data.order_list, r = n[a].goods_list[i], c = r.has_many_options;
        r.check_spe[s] = o;
        var d = [];
        r.has_many_specs.forEach(function(t, e) {
            var a = r.check_spe[e];
            a >= 0 && d.push(t.specitem[a].title);
        }), d = d.join("+"), c.forEach(function(t) {
            t.title == d && (r.price = t.product_price, r.market_price = t.market_price, r.spe_name = t.id);
        }), this.setData({
            order_list: n
        }), this.totalPrice();
    },
    totalPrice: function() {
        var t = 0, e = this.data.order_list, a = 0;
        if (e.forEach(function(e) {
            var i = !1;
            e.goods_list.forEach(function(e) {
                e.check && (i = !0), t += e.check ? e.price * e.count : 0;
            }), i && (a += 1);
        }), a == e.length && this.data.time_over) {
            var i = t - Number(this.data.on_sale_price);
            t = i < 0 ? 0 : i, this.setData({
                isDiscount: !0
            });
        } else this.setData({
            isDiscount: !1
        });
        t = t.toFixed(2), this.setData({
            total: t
        });
    },
    shopChange: function(t) {
        var e = t.target, a = t.detail, i = e.dataset, s = i.i, o = i.c_index, n = i.count, r = this.data.order_list, c = r[s].goods_list[o];
        n ? c.count = a : c.check = !c.check, this.setData({
            order_list: r
        }), this.totalPrice();
    },
    gopackgoods: function(t) {
        var e = t.currentTarget.dataset.id;
        this.setData({
            loadingHidden: !1
        }), this.getdata(e), wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        });
    },
    getdata: function(e) {
        var a = this, i = t.getNetAddresss("plugin.goods-package.frontend.package.package.index");
        t._postNetWork({
            showToastIn: !1,
            url: i,
            data: {
                package_id: e
            },
            success: function(t) {
                var i = t.data;
                if (i.result) {
                    var s = i.data, o = s.categories, n = s.title, r = s.description_thumb, c = s.description_title, d = s.description_desc, l = s.carousels, _ = s.on_sale_price, h = s.end_time, p = s.share_title, u = s.share_desc, g = s.share_thumb, f = s.limit_time_status, m = s.other_packages, v = s.page_template, k = o.map(function(t) {
                        return t.goods_list.map(function(t) {
                            return t.count = 1, t.check = !!v, t.check_spe = [], t.price = t.market_price, t.spe_name = v && 1 == t.has_option && t.has_many_options[0] ? t.has_many_options[0].id : 0, 
                            t;
                        }), t;
                    });
                    h = 1e3 * (h - parseInt(new Date().getTime() / 1e3)), a.setData({
                        title: n,
                        description_thumb: r,
                        order_list: k,
                        description_title: c,
                        description_desc: d,
                        carousels: l,
                        on_sale_price: _,
                        end_time: h,
                        package_id: e,
                        share_title: p,
                        share_desc: u,
                        share_thumb: g,
                        time_over: 1 == f,
                        other_packages: m,
                        loadingHidden: !0,
                        page_template: v
                    }), a.getTotal();
                } else wx.showToast({
                    icon: "none",
                    title: i.msg,
                    duration: 1500
                });
            }
        });
    },
    todetail: function(t) {
        var e = t.currentTarget;
        this.data.page_template || wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + e.dataset.id
        });
    },
    onLoad: function(t) {
        var e = t.pid;
        this.setData({
            pid: e
        });
    },
    onReady: function() {},
    onShow: function() {
        this.getdata(this.data.pid);
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), e = "";
        return t && (e = t), {
            path: "/packageF/packageGoodse/packageGoods?pid=" + this.data.package_id + "&mid=" + e,
            title: this.data.share_title,
            imageUrl: this.data.share_thumb
        };
    }
});